package com.ygl.zuul;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.ZuulFilterResult;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ygl
 * @create 2019-01-24
 * @DESCRIPTION
 **/
@Component
public class TokenFilter extends ZuulFilter {


    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String token = request.getParameter("token");
//        if (StringUtils.isEmpty(token)) {
//            //不会继续执行
//            currentContext.setSendZuulResponse(false);
//            currentContext.setResponseBody("token is null");
//            currentContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
//            return null;
//        }
        /**
         * 正常执行其他的
         */
        return null;
    }

    /**
     * 请求前执行
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }
}
